Chart3D for WPF and Silverlight > Using Chart3D for WPF and Silverlight > Creating a Custom Axis Annotation > Custom Axis Annotation Template |
The following sample shows how to use the AnnoTemplate property to create axis labels with color depending on the corresponding axis value.
Add the following XAML to your MainWindow.xaml or your MainPage.xaml:
Note: In a Silverlight application, you'll add <Page.Resources> </Page.Resources> tags instead of the <Window.Resources> tags. |
XAML |
Copy Code
|
---|---|
<Window.Resources> <!-- instance of converter --> <local:LabelToColorConvereter x:Key="cnv" /> </Window.Resources> <Grid> <c1chart3d:C1Chart3D Name="c1Chart3D1" > <c1chart3d:GridDataSeries ZDataString="-1 -1 -1,0 0 0, 1 1 1" /> <c1chart3d:C1Chart3D.AxisZ> <c1chart3d:Axis3D> <!-- set axis annotation template --> <c1chart3d:Axis3D.AnnoTemplate> <DataTemplate > <!-- DataContext is axis label(string), use converter to set color --> <TextBlock Width="20" Height="12" Text="{Binding}" Foreground="{Binding Converter={StaticResource cnv}}" /> </DataTemplate> </c1chart3d:Axis3D.AnnoTemplate> </c1chart3d:Axis3D> </c1chart3d:C1Chart3D.AxisZ> </c1chart3d:C1Chart3D> </Grid> |
Select View | Code and add the following code. The binding converter selects the brush of the axis label depending on the value.
C# |
Copy Code
|
---|---|
public class LabelToColorConvereter : IValueConverter { static Brush belowZero = new SolidColorBrush(Colors.Blue); static Brush aboveZero = new SolidColorBrush(Colors.Red); static Brush zero = new SolidColorBrush(Colors.DarkGray); public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // string representing axis label string s = value as string; if (!string.IsNullOrEmpty(s)) { var dv = double.Parse(s); // return brush depending on the value if (dv < 0) return belowZero; else if (dv > 0) return aboveZero; else return zero; } return value; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } |